'use strict';


const Service = require('egg').Service;
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT')
class CardService extends Service {
  async list(params) {
    let result = {
      count: "",
      rows: ""
    }
    result.count = await this.ctx.model.Card.find({
      oltId: params.OltId
    }).count('_id')
    result.rows = await this.ctx.model.Card.find({
      oltId: params.OltId
    }).sort('slot').skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    return result
  }
  async delete(params) {
    let result = {
      msg: '',
      flag: false
    }
    //这里默认是成功状态
    let cardInformation = await this.ctx.model.Card.findOne({
      _id: params.id
    })
    //查询olt设备信息拼装option
    try {
      // let oltInformation = await this.ctx.model.Olt.findOne({_id:cardInformation.oltId});
      let oltInformation = await this.ctx.service.Common.getOltInfo(cardInformation.oltId);
      await deleteCard(cardInformation, oltInformation, this)
      await this.ctx.model.Card.deleteOne({
        _id: params.id
      })
      result.msg = 'delete card successful'
      result.flag = true
      return result
    } catch (error) {
      result.msg = 'delete card fail,' + error
      result.flag = false
      return result
    }
  }
  async create(params) {
    let result = {
      msg: '',
      flag: false
    }
    //这里默认是成功状态
    let cardInformation = await this.ctx.model.Card.findOne({
      _id: params.id
    })
    //查询olt设备信息拼装option
    let oltInformation = await this.ctx.model.Olt.findOne({
      _id: cardInformation.oltId
    });
    try {
      //操作添加card
      await createCard(cardInformation, oltInformation, this)
      switch (oltInformation.oltType) {
        case "ZTE":
          let type = cardInformation.realType
          if (type.indexOf('GTGH') != -1) {
            type = "GTGH"
          }
          await this.ctx.model.Card.updateOne({
            _id: params.id
          }, {
            $set: {
              status: "INSERVICE",
              type: type
            }
          })
          break;
        case "HUAWEI":
          await this.ctx.model.Card.updateOne({
            _id: params.id
          }, {
            $set: {
              status: "Normal",
            }
          })
          break;
      }
      result.msg = 'card create successful'
      result.flag = true
      return result
    } catch (error) {
      result.msg = 'card create fail,' + error
      result.flag = false
      return result
    }
  }
}
async function createCard(cardResult, oltInformation, me) {
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName, oltInformation.RZUsername, oltInformation.RZPassword, oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.createCard(cardResult.slot, cardResult.realType, oltInformation.oltHardVersion, me)
        break;
      case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.createCard(cardResult.slot, me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function deleteCard(cardResult, oltInformation, me) {
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName, oltInformation.RZUsername, oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        const zte = new ZTEOLT(options);
        result = await zte.deleteCard(cardResult.slot, oltInformation.oltHardVersion, me)
        break;
      case "HUAWEI":
        const hw = new HWOLT(options);
        result = await hw.deleteCard(cardResult.slot, me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}

module.exports = CardService;